<template>
	<view class="height-screen">
		<view class="absolute" style="height: 100vh;width: 100vw;background: rgba(6,193,104,0.15);" />
		<cu-custom isBack>
			<block slot="content">{{$t('mine.pullNew')}}</block>
		</cu-custom>
		<view class="grid margin-ht" style="gap: 50rpx;margin-top: 20rpx;">
			<view class="flex justify-end align-center" @tap.stop="helper.routerNav('/pages/mine/pullNewDetail')">
				<image src="@/static/img/detail.png" style="width: 23rpx;height: 28rpx;" />
				<text class="text-df font-400 margin-left-sm"
					style="color: #06C168;">{{$t('filed.myPullDetail')}}</text>
			</view>
			<image :src="finalPath" style="height: 70vh;width: 100%;" />
			<view class="grid" style="gap: 50rpx;height: 80rpx;">
				<view class="setButton" @tap.stop="saveImage()"
					style="background: #06C168;box-shadow: 0rpx 5rpx 7rpx 0rpx rgba(0,0,0,0.14);border-radius: 40rpx;">
					<text class="text-df text-white font-400">{{$t('filed.down')}}</text>
				</view>
			</view>
		</view>
		<view class="hideCanvasView">
			<canvas class="hideCanvas" canvas-id="default_PosterCanvasId"
				:style="{width: (posterInfo.width) + 'px', height: (posterInfo.height) + 'px'}"></canvas>
		</view>
	</view>
</template>

<script>
	import {
		poster,
		getInfo
	} from '@/apis'
	import {
		getSharePoster
	} from '@/components/QS-SharePoster/QS-SharePoster.js';
	export default {
		data() {
			return {
				posterInfo: {},
				finalPath: ''
			}
		},
		onShow() {
			this.shareFc()
		},
		methods: {
			async shareFc() {
				const res = await poster()
				const userInfo = await getInfo()
				const d = await getSharePoster({
					_this: this,
					type: 'testShareType',
					backgroundImage: this.helper.handleImage(res),
					posterCanvasId: 'default_PosterCanvasId', //canvasId
					delayTimeScale: 20,
					drawArray: ({
						bgObj,
						type,
						bgScale
					}) => {
						return new Promise((rs, rj) => {
							rs([{
								type: 'qrcode',
								text: `https://www.slem.com.au/web/index.html#/pages/mine/pullNewUser?pid=${userInfo.id}`,
								size: bgObj.width * 0.2,
								dx: bgObj.width * 0.70,
								dy: bgObj.height - bgObj.width * 0.25
							}]);
						})
					},
					setCanvasWH: ({
						bgObj,
						type,
						bgScale
					}) => { // 为动态设置画布宽高的方法，
						this.posterInfo = bgObj;
					}
				});
				this.finalPath = d.poster.tempFilePath;
			},
			saveImage() {
				uni.saveImageToPhotosAlbum({
					filePath: this.finalPath,
					success: (res) => {
						this.helper.showToast(this.$t('message.success'))
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.hideCanvasView {
		position: relative;
	}

	.hideCanvas {
		position: fixed;
		top: -99999upx;
		left: -99999upx;
		z-index: -99999;
	}
</style>